//
//  QuDanUserCetnerMainController.m
//  QuDanOEM
//
//  Created by xiaohuang on 2021/11/7.
#import "QuDanUserCetnerMainController.h"
#import "QuDanUserCenterSendController.h"
#import "QuDanUserCenterDynamicController.h"
#import "QuDanUserCetnerTopView.h"
#import "QuDanUserCeterHeaderView.h"
#import "QuDanUserCenterSendController.h"
#import "QuDanTestXibVIew.h"
@interface QuDanUserCetnerMainController ()<HyCycleViewProviderProtocol>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) HyCycleView *cycleView;
@property (nonatomic,strong) HySegmentView *segmentView;
@property (nonatomic,strong) HyCyclePageView *cyclePageView;
@property (nonatomic,strong) NSArray<NSString *> *titleArray;
@property (nonatomic,strong) NSArray<UIColor *> *colorArray;
@property (nonatomic,strong)QuDanUserCetnerTopView *topView;
@property (nonatomic,strong)UIImageView *bgImage;
@end
@implementation QuDanUserCetnerMainController
- (UIImageView *)bgImage
{
    if (!_bgImage) {
        _bgImage = [[UIImageView alloc]init];
        _bgImage.contentMode =  UIViewContentModeScaleAspectFill;
        _bgImage.image = [UIImage imageNamed:@"mine_25"];
    }
    return _bgImage;
}
- (QuDanUserCetnerTopView *)topView
{
    if (!_topView) {
        _topView = [[QuDanUserCetnerTopView alloc]init];
        _topView.backgroundColor = [UIColor clearColor];
    }
    return _topView;
}
/// 隐藏导航栏
- (SAViewControllerNaviType)customNaviType{
    return SAViewControllerNaviTypeHide;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.bgImage];
    [self.bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.offset(300);
    }];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(Height_NavBar);
    }];
    self.titleArray = @[@"发单信息",@"动态"];
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(Height_NavBar);
    }];
    [self.cyclePageView reloadData];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.cyclePageView.heightValue(self.scrollView.height);
}
- (UIView *)hoverView {
    return self.segmentView;
}
- (UIView *)headerView
{
    QuDanTestXibVIew *xibView = [[NSBundle mainBundle]loadNibNamed:@"QuDanTestXibVIew" owner:nil options:nil].firstObject;
    xibView.size =  CGSizeMake(self.view.width, 260);
    return xibView;
}
- (void (^)(HyCyclePageViewConfigure * _Nonnull))configPageView {
    return ^(HyCyclePageViewConfigure * _Nonnull configure){

        [[[configure headerRefreshPositon:HyCyclePageViewHeaderRefreshPositionCenter] headerRefreshAtIndex:^(HyCyclePageView * _Nonnull cyclePageView, UIScrollView * _Nonnull scrollView, id  _Nonnull view, NSInteger index) {
            NSLog(@"index:%ld",(long)index);
            __block typeof(scrollView) weakScrollView = scrollView;
            scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                NSLog(@"indexindex:%ld",(long)index);
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
                                             (int64_t)(.5 * NSEC_PER_SEC)),
                               dispatch_get_main_queue(), ^{

                    [weakScrollView.mj_header endRefreshing];
                });
            }];
        }] footerRefreshAtIndex:^(HyCyclePageView * _Nonnull cyclePageView, UIScrollView * _Nonnull scrollView, id  _Nonnull view, NSInteger index) {
            NSLog(@"footer:%ld",(long)index);
            __block typeof(scrollView) weakScrollView = scrollView;
            scrollView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
                                             (int64_t)(.5 * NSEC_PER_SEC)),
                               dispatch_get_main_queue(), ^{

                                   [weakScrollView.mj_footer endRefreshing];
                               });
            }];
        }];
    };
}
- (void (^)(HySegmentViewConfigure *))configSegmentView {
    return nil;
}
#pragma mark — HyCycleViewProviderProtocol
- (void)configCycleView:(HyCycleViewProvider<HyCycleView *> *)provider index:(NSInteger)index {
    NSArray *imageArray = @[@"one",@"two",@"three",@"four"];
    [provider view:^UIView * _Nonnull(HyCycleView * _Nonnull cycleView) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageArray[index]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        return imageView;
    }];
}
#pragma mark — getters and setters
- (UIScrollView *)scrollView {
    if (!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.backgroundColor = [UIColor clearColor];
        [_scrollView addSubview:self.cyclePageView];
    }
    return _scrollView;
}

- (HyCyclePageView *)cyclePageView {
    if (!_cyclePageView){
         __weak typeof(self) _self = self;
        _cyclePageView = [[HyCyclePageView alloc] initWithFrame:_scrollView.bounds];
        [[[[[[_cyclePageView.configure totalIndexs:^NSInteger(HyCyclePageView * _Nonnull cycleView) {
            __strong typeof(_self) self = _self;
            return self.titleArray.count;
        }] viewProviderAtIndex:^id<HyCyclePageViewProviderProtocol> _Nonnull(HyCyclePageView * _Nonnull cycleView, NSInteger index) {
            __strong typeof(_self) self = _self;
            QuDanUserCenterSendController *vc = QuDanUserCenterSendController.new;
            vc.scrollViewDidEndDragging = self.scrollViewDidEndDragging;
            return vc;
        }] scrollProgress:^(HyCyclePageView * _Nonnull cycleView, NSInteger fromIndex, NSInteger toIndex, CGFloat progress) {
            __strong typeof(_self) self = _self;
            [self.segmentView clickItemFromIndex:fromIndex
                                         toIndex:toIndex
                                        progress:progress];
        }] headerView:self.headerView] hoverView:self.hoverView] loadStyle:HyCycleViewLoadStyleDidAppear];

        !self.configPageView ?: self.configPageView(_cyclePageView.configure);
    }
    return _cyclePageView;
}

- (HySegmentView *)segmentView {
    if (!_segmentView){

        __weak typeof(self) _self = self;
        _segmentView =
        [HySegmentView segmentViewWithFrame:CGRectMake(0, 0, self.view.width, 40)
                             configureBlock:^(HySegmentViewConfigure * _Nonnull configure) {

            __strong typeof(_self) self = _self;
            configure
            .numberOfItems(self.titleArray.count)
            .itemMargin(25)
            .viewForItemAtIndex(^UIView *(UIView *currentView,
                                          NSInteger currentIndex,
                                          CGFloat progress,
                                          HySegmentViewItemPosition position,
                                          NSArray<UIView *> *animationViews){
                __strong typeof(_self) self = _self;
                UILabel *label = (UILabel *)currentView;
                if (!label) {
                    label = [UILabel new];
                    label.text = self.titleArray[currentIndex];
                    label.textAlignment = NSTextAlignmentCenter;
                    [label sizeToFit];
                }
                if (progress == 0 || progress == 1) {
                    label.textColor =  progress == 0 ? UIColor.grayColor : UIColor.blackColor;
                }
                return label;
            })
             .animationViews(^NSArray<UIView *> *(NSArray<UIView *> *currentAnimations, UICollectionViewCell *fromCell, UICollectionViewCell *toCell, NSInteger fromIndex, NSInteger toIndex, CGFloat progress){

                 NSArray<UIView *> *array = currentAnimations;
                 if (!array.count) {
                     UIView *line = [UIView new];
                     line.backgroundColor = UIColor.clearColor;
                     line.layer.cornerRadius = 1.5;
                     line.heightValue(3).bottomValue(40);
                     array = @[line];
                 }

                 CGFloat margin = ABS(toCell.centerX - fromCell.centerX);
                 CGFloat currentProgress = progress <= 0.5 ? progress : (1 - progress);
                 CGFloat width = 15;
                 array.firstObject.widthValue(width + margin * currentProgress * 2);

                 if (fromIndex < toIndex) {
                     if (progress <= 0.5) {
                         array.firstObject.leftValue(fromCell.centerX - width / 2);
                     } else {
                         array.firstObject.rightValue(toCell.centerX + width / 2);
                     }
                 } else {
                     if (progress <= 0.5) {
                         array.firstObject.rightValue(fromCell.centerX + width / 2);
                     } else {
                         array.firstObject.leftValue(toCell.centerX - width / 2);
                     };
                 }

                 return array;
             })
            .clickItemAtIndex(^BOOL(NSInteger currentIndex, BOOL isRepeat){
                __strong typeof(_self) self = _self;
                if (!isRepeat) {
                    [self.cyclePageView scrollToIndex:currentIndex animated:YES];
                }
                return NO;
            });
            !self.configSegmentView ?: self.configSegmentView(configure);
        }];
    }
    return _segmentView;
}

@end
